﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Clmp.WebForm
{
    static class ControlMapping
    {
        public static void SetControlValue(Control control, object value)
        {
            switch (control.GetType().Name)
            {
                case "TextBox": ((TextBox)control).Text = value.ToString();
                    break;
                case "Label": ((Label)control).Text = value.ToString();
                    break;
                case "DropDownList": ((DropDownList)control).SelectedValue = value.ToString();
                    break;
                case "ListBox": ((ListBox)control).SelectedValue = value.ToString();
                    break;
                case "CheckBox": ((CheckBox)control).Checked = (bool)value;
                    break;
                case "RadioButtonList": ((RadioButtonList)control).SelectedValue = value.ToString();
                    break;
                case "Literal": ((Literal)control).Text = value.ToString();
                    break;
                default:
                    throw new Exception("为实现此类型支持。");
            }
        }

        public static object GetControlValue(Control control, Type rtnType)
        {
            object value;
            switch (control.GetType().Name)
            {
                case "TextBox": value = ((TextBox)control).Text;
                    break;
                case "Label": value = ((Label)control).Text;
                    break;
                case "DropDownList": value = ((DropDownList)control).SelectedValue;
                    break;
                case "ListBox": value = ((ListBox)control).SelectedValue;
                    break;
                case "CheckBox": value = ((CheckBox)control).Checked;
                    break;
                case "RadioButtonList": value = ((RadioButtonList)control).SelectedValue;
                    break;
                case "Literal": value = ((Literal)control).Text;
                    break;
                default:
                    throw new Exception("为实现此类型支持。");
            }

            if (rtnType == value.GetType())
            {
                return value;
            }
            value = GetValue(rtnType, value.ToString());
            return value;
        }

        public static bool FilterGetControl(Control control)
        {
            switch (control.GetType().Name)
            {
                case "TextBox":
                    return true;
                case "DropDownList":
                    return true;
                case "ListBox":
                    return true;
                case "CheckBox":
                    return true;
                case "RadioButtonList":
                    return true;
            }

            return false;
        }

        private static object GetValue(Type rtnType, string str)
        {
            if (rtnType == typeof(string))
                return str;

            if (!rtnType.IsValueType)
                throw new ArithmeticException("转换类型 T 必须是一个值类型。");

            if (rtnType.IsEnum)
            {
                return Enum.Parse(rtnType, str, true);
            }
            //可空类型
            if (rtnType.Name.IndexOf("Nullable") >= 0)
            {
                var type = rtnType.GetMethod("get_Value").ReturnType;
                return GetValue(type,str);
            }

            var method = rtnType.GetMethod("Parse", new[] { typeof(string) });

            if (method == null)
                throw new ArithmeticException("转换类型 T 必须包含Parse方法。");

            return method.Invoke(null, new object[] { str });
        }
    }
}
